<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { artGetListService, artDelService } from '@/api/article'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
// 假数据
const articleList = ref([])
const total = ref(0)
const loading = ref(false)
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
const articleEditRef = ref()
// 获取文章列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  // console.log(res)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList()
// 处理分页逻辑
const handleSizeChange = (size) => {
  // console.log(size) // 当前每条页数
  // 只要是每页条数发生变化了, 那么原本正在访问的当前页意义就不大了, 数据大概率已经不在原来那一页了
  // 重新从第一页渲染即可
  params.value.pagenum = 1
  params.value.pagesize = size
  // 基于最新的的当前页 和 每页的条数渲染
  getArticleList()
}
const handleCurrentChange = (page) => {
  // console.log(page)
  // 更新当前页
  params.value.pagenum = page
  getArticleList()
}
// 搜索 => 按照最新的条件,重新检索, 从第一页开始
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}
// 重置 => 将筛选条件清空, 重新检索, 从第一页开始
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
// 编辑
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
  // console.log(row)
}
// 删除
const onDeleteArticle = async (row) => {
  console.log(row)
  await ElMessageBox.confirm('你确认删除文章信息吗?', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage.success('删除成功')
  getArticleList()
}
// 添加文章
const onAddArticle = () => {
  // visibleDrawer.value = true
  articleEditRef.value.open({})
}
// 添加成功
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加 需要跳转到最后一页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button @click="onAddArticle" type="primary">发布文章</el-button>
    </template>
    <!-- 主体部分 = 表单区域 + 表格区域 + 分页-->
    <!-- 表单 -->
    <!-- inline 会横排 el-form-item -->
    <el-form inline>
      <el-form-item label="文章分类:">
        <!-- 文章分类的下拉菜单 -->
        <!-- v-model 是 :modeValue 和 @update:modeValue 的简写 -->
        <channel-select v-model="params.cate_id" width="200px"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态:">
        <el-select style="width: 200px">
          <!-- 这里后台标记发布状态, 就是通过中文标记的 -->
          <el-option
            label="已发布"
            value="已发布"
            v-model="params.state"
          ></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="articleList" v-loading="loading">
      <!-- 超链接 用具名插槽-->
      <el-table-column lable="文章标题" width="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column lable="分类" prop="cate_name"></el-table-column>
      <el-table-column lable="发布时间" prop="pub_date">
        <!-- 时间格式化 调用函数 -->
        <template #default="{ row }"> {{ formatTime(row.pub_date) }}</template>
      </el-table-column>
      <el-table-column lable="状态" prop="state"></el-table-column>
      <el-table-column lable="操作">
        <!-- 自定义列 -->
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 7, 10]"
      background
      layout=" jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <!-- 抽屉容器 将来用组件 -->
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
  </page-container>
</template>
